package com.aaa.tars.controller;

import com.aaa.tars.entity.Scenic;
import com.aaa.tars.service.ScenicService;
import com.aaa.tars.util.OssUtil;
import com.aaa.tars.util.Page;
import com.aaa.tars.util.Result;
import com.aaa.tars.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/scenic")
public class ScenicController {
    @Resource
    private ScenicService scenicService;

    // 新增：查询景区及下属所有门票（关联查询接口）
    @GetMapping("queryWithTickets")
    public Result queryScenicWithTickets(Integer scenicId) {
        Scenic scenic = scenicService.queryScenicWithTickets(scenicId);
        if (scenic != null) {
            return ResultUtil.success(scenic);
        } else {
            return ResultUtil.error("未找到相关景区信息");
        }
    }

    // 新增：查询景区及下属所有景点（关联查询接口）
    @GetMapping("queryScenicWithDots")
    public Result queryScenicWithDots(Integer scenicId) {
        Scenic scenic = scenicService.queryScenicWithDots(scenicId);
        if (scenic != null) {
            return ResultUtil.success(scenic);
        } else {
            return ResultUtil.error("为找到相关景区信息");
        }
    }

    @GetMapping("queryAll")
    public Result queryAll() {
        return ResultUtil.success(scenicService.queryAll());
    }

    @GetMapping("frontQueryAll")
    public Result frontQueryAll() {
        return ResultUtil.success(scenicService.frontQueryAll());
    }

    // 新增：查询景区及下属所有门票和景点（完整关联查询接口）
    @GetMapping("queryScenicWithTicketsAndDots")
    public Result queryScenicWithTicketsAndDots(Integer scenicId) {
        Scenic scenic = scenicService.queryScenicWithTicketsAndDots(scenicId);
        if (scenic != null) {
            return ResultUtil.success(scenic);
        } else {
            return ResultUtil.error("未找到相关景区信息");
        }
    }

    // 分页查询接口
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody Page<Scenic> page) {
        try {
            PageInfo pageInfo = scenicService.queryByPage(page);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询失败: " + e.getMessage());
        }
    }


    @PostMapping("add")
    public Result add(@RequestBody Scenic scenic) {
        return ResultUtil.success(scenicService.add(scenic));
    }

    @PostMapping("update")
    public Result update(@RequestBody Scenic scenic) {
        return ResultUtil.success(scenicService.update(scenic));
    }

    // 删除景区接口
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Integer scenicId) {
        try {
            int result = scenicService.deleteById(scenicId);
            if (result > 0) {
                return ResultUtil.success("删除成功");
            } else {
                return ResultUtil.error("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("删除失败: " + e.getMessage());
        }
    }

    // 根据ID查询接口
    @GetMapping("/queryById")
    public Result queryById(@RequestParam Integer scenicId) {
        try {
            Scenic scenic = scenicService.queryById(scenicId);
            return ResultUtil.success(scenic);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public Result uploadImage(@RequestParam("file") MultipartFile file) {
        return scenicService.uploadImage(file);
    }
}